/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/6/7 14:19:07
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KouSuan.Models;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using KouSuan.Classes;

namespace KouSuan.ViewModels
{
    public class ExamViewModel : ViewModelBase
    {
        public ExamViewModel()
        {

        }

        private System.Threading.Timer timer;

        private IList<ExpressionModel> _Expressions;

        public IList<ExpressionModel> Expressions
        {
            get { return _Expressions; }
            set
            {
                if (_Expressions != value)
                {
                    _Expressions = value;
                    RaisePropertyChanged(() => Expressions);
                }
            }
        }

        private int _Count = 50;

        public int Count
        {
            get { return _Count; }
            set { _Count = value; }
        }

        private List<ChutiType> _ChutiType;

        public List<ChutiType> ChutiType
        {
            get
            {
                if (_ChutiType == null)
                {
                    _ChutiType = new List<ChutiType> { 
                        new ChutiType{ ExaminationType=ExaminationType.FiveAddition, Name="5以内加法"},
                        new ChutiType{ ExaminationType=ExaminationType.FiveSubstraction, Name="5以内减法"},
                        new ChutiType{ ExaminationType=ExaminationType.FiveAll, Name="5以内加减法"},
                        new ChutiType{ ExaminationType=ExaminationType.TenAddition, Name="10以内加法"},
                        new ChutiType{ ExaminationType=ExaminationType.TenSubstraction, Name="10以内减法"},
                        new ChutiType{ ExaminationType=ExaminationType.TenAll, Name="10以内加减法"},
                        new ChutiType{ ExaminationType=ExaminationType.TwentyAddition, Name="20以内加法"},
                        new ChutiType{ ExaminationType=ExaminationType.TwentySubstraction, Name="20以内减法"},
                        new ChutiType{ ExaminationType=ExaminationType.TwentyCarrySaveAddition, Name="20以内进位加法"},
                        new ChutiType{ ExaminationType=ExaminationType.TwentyAbdicationSubtraction, Name="20以内退位减法"},
                        new ChutiType{ ExaminationType=ExaminationType.TwentyAll, Name="20以内加减法"},
                        new ChutiType{ ExaminationType=ExaminationType.HundredAddition, Name="100以内加法"},
                        new ChutiType{ ExaminationType=ExaminationType.HundredSubstraction, Name="100以内减法"},
                        new ChutiType{ ExaminationType=ExaminationType.HundredCarrySaveAddition, Name="100以内进位加法"},
                        new ChutiType{ ExaminationType=ExaminationType.HundredAbdicationSubtraction, Name="100以内退位减法"},
                        new ChutiType{ ExaminationType=ExaminationType.HundredAll, Name="100以内加减法"},
                    };
                }
                return _ChutiType;
            }
        }

        private ExaminationType _ChutiTypeSelected;

        public ExaminationType ChutiTypeSelected
        {
            get { return _ChutiTypeSelected; }
            set { _ChutiTypeSelected = value; }
        }

        private TimeSpan _LeftTime;

        public TimeSpan LeftTime
        {
            get { return _LeftTime; }
            set
            {
                if (value != _LeftTime)
                {
                    _LeftTime = value;
                    RaisePropertyChanged(() => LeftTime);
                }
            }
        }

        private TimeSpan _TotalTime = TimeSpan.FromMinutes(10);

        public TimeSpan TotalTime
        {
            get { return _TotalTime; }
            set
            {
                if (value != _TotalTime)
                {
                    _TotalTime = value;
                    RaisePropertyChanged(() => TotalTime);
                }
            }
        }

        private bool _Examing;

        /// <summary>
        /// 是否正在考试中
        /// </summary>
        public bool Examing
        {
            get { return _Examing; }
            set
            {
                if (value != _Examing)
                {
                    _Examing = value;
                    RaisePropertyChanged(() => Examing);
                }
            }
        }

        private bool _Checked = false;
        /// <summary>
        /// 是否已经判过卷
        /// </summary>
        public bool Checked
        {
            get { return _Checked; }
            set
            {
                if (value != _Checked)
                {
                    _Checked = value;
                    RaisePropertyChanged(() => Checked);
                }
            }
        }

        private string _Result = "00/00";

        public string Result
        {
            get { return _Result; }
            set
            {
                if (_Result != value)
                {
                    _Result = value;
                    RaisePropertyChanged(() => Result);
                }
            }
        }


        private ICommand _ChuTiCommand;
        /// <summary>
        /// 出题命令
        /// </summary>
        public ICommand ChuTiCommand
        {
            get
            {
                if (_ChuTiCommand == null)
                {
                    _ChuTiCommand = new RelayCommand(() =>
                    {
                        ExaminationType chutitype = ChutiTypeSelected;
                        int c = _Count;
                        var ee = GenerateQuestionsEngine.Generate(_Count, chutitype);

                        for (int i = 0; i < ee.Count; i++)
                        {
                            ee[i].TabIndex = i;
                        }

                        Expressions = ee;

                        LeftTime = TotalTime;
                    });
                }
                return _ChuTiCommand;
            }
        }

        private ICommand _SubmitCommand;
        /// <summary>
        /// 交卷命令
        /// </summary>
        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(
                        () =>
                            {
                                Checked = true;
                                if (Expressions != null)
                                {
                                    foreach (var item in Expressions)
                                    {
                                        item.Check();
                                    }
                                    Result = string.Format("{0}/{1}", Expressions.Count(p => p.IsRight), Expressions.Count);
                                }
                                timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                                Examing = false; 
                            }
                        );
                }
                return _SubmitCommand;
            }
        }

        private ICommand _StartCommand;
        /// <summary>
        /// 开始考试命令
        /// </summary>
        public ICommand StartCommand
        {
            get
            {
                if (_StartCommand == null)
                {
                    _StartCommand = new RelayCommand(() =>
                    {
                        Examing = true;
                        if (timer == null)
                        {
                            timer = new System.Threading.Timer((o) =>
                            {
                                LeftTime = LeftTime.Subtract(TimeSpan.FromSeconds(1));
                            }, null, 1000, 1000);
                        }
                        else
                        {
                            timer.Change(1000, 1000);
                        }
                    });
                }
                return _StartCommand;
            }
        }

    }

    public class ChutiType
    {
        public ExaminationType ExaminationType { get; set; }
        public string Name { get; set; }
    }
}
